package com.housediyadmin.service
{
	import com.housediyadmin.events.fittingEvents.GetFittingEvent;
	import com.housediyadmin.vo.Fitting;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.messaging.ChannelSet;
	import mx.messaging.channels.AMFChannel;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	import org.robotlegs.mvcs.Actor;
	
	/**
	 * 
	 * @author yanghanbing
	 * 2010/12/15
	 */	
	public class FittingRemoteObjectService extends Actor
	{
		public var fittingRemoteObject:RemoteObject;
		
		public function FittingRemoteObjectService()
		{
			super();
			
			var amfChannel:Array = [new AMFChannel(null, "http://localhost/housediyadmin/Gateway.aspx")];
			//var amfChannel:Array = [new AMFChannel(null, "http://10.60.1.120/HouseDiyAdmin/Gateway.aspx")];
			var channelSet:ChannelSet = new ChannelSet();
			channelSet.channels = amfChannel;
			
			fittingRemoteObject = new RemoteObject("fluorine");
			fittingRemoteObject.source = "HouseDiyService.HouseDiyAction";
			fittingRemoteObject.showBusyCursor = true;
			fittingRemoteObject.channelSet = channelSet;
			
			fittingRemoteObject.getOperation("getAllFittings").addEventListener(ResultEvent.RESULT, getAllFittingsResult);
			fittingRemoteObject.getOperation("getAllFittings").addEventListener(FaultEvent.FAULT, faultHandler);
			
			fittingRemoteObject.getOperation("deleteFittingByIDs").addEventListener(ResultEvent.RESULT, deleteFittingResult);
			fittingRemoteObject.getOperation("deleteFittingByIDs").addEventListener(FaultEvent.FAULT, faultHandler);
			
			/*fittingRemoteObject.getOperation("editAFitting").addEventListener(ResultEvent.RESULT, editFittingResult);
			fittingRemoteObject.getOperation("editAFitting").addEventListener(FaultEvent.FAULT, faultHandler);*/
			
			fittingRemoteObject.getOperation("addAFitting").addEventListener(ResultEvent.RESULT, addFittingResult);
			fittingRemoteObject.getOperation("addAFitting").addEventListener(FaultEvent.FAULT, faultHandler);
			
			/*fittingRemoteObject.getOperation("getFittingsByGoodsID").addEventListener(ResultEvent.RESULT, getFittingByGoodsIDResult);
			fittingRemoteObject.getOperation("getFittingsByGoodsID").addEventListener(FaultEvent.FAULT, faultHandler);	*/
		}
		
		public function getAllFitting():void
		{
			fittingRemoteObject.getAllFittings();
		}
		
		public function deleteFittingByID(fittingId:Array):void
		{
			fittingRemoteObject.deleteFittingByIDs(fittingId);
		}
		
		public function addFitting(fitting:Fitting, goodsIds:Array = null):void
		{
			fittingRemoteObject.addAFitting(fitting, goodsIds);
		}
		
		public function editFitting(fitting:Fitting, goodsIds:Array = null):void
		{
			fittingRemoteObject.editAFitting(fitting, goodsIds);
		}
				
//		public function getFittingByGoodsID(goodsID:int):void
//		{
//			fittingRemoteObject.getFittingsByGoodsID(goodsID);
//		}
		
		private function getAllFittingsResult(event:ResultEvent):void
		{
			dispatch(new GetFittingEvent(GetFittingEvent.UPDATE_FITTING_MODEL, event.result as ArrayCollection));
		}
		
		private function deleteFittingResult(event:ResultEvent):void
		{
			dispatch(new GetFittingEvent(GetFittingEvent.UPDATE_FITTING_MODEL, event.result as ArrayCollection));
		}
		
		private function editFittingResult(event:ResultEvent):void
		{
			dispatch(new GetFittingEvent(GetFittingEvent.UPDATE_FITTING_MODEL, event.result as ArrayCollection));
		}
		
		private function addFittingResult(event:ResultEvent):void
		{
			dispatch(new GetFittingEvent(GetFittingEvent.UPDATE_FITTING_MODEL, event.result as ArrayCollection));
		}
		
		private function getFittingByGoodsIDResult(event:ResultEvent):void
		{
			dispatch(new GetFittingEvent(GetFittingEvent.UPDATE_FITTING_GOODS, event.result as ArrayCollection));
		}
		
		private function faultHandler(event:FaultEvent):void
		{
			Alert.show(" fault :" + event.toString());
		}
	}
}